package xing.appwidget.main

import android.os.Bundle
import android.view.Gravity
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import xing.appwidget.R
import xing.appwidget.databinding.ActivityMainBinding
import xing.appwidget.event.AppFilterUpdateEvent
import xing.appwidget.label.LabelManagerFragment
import xing.appwidget.utils.DisplayUtil
import xing.appwidget.utils.Utils
import java.lang.StringBuilder

class MainActivity : AppCompatActivity() {

    private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
    private val mainViewModel by lazy { ViewModelProvider(this)[MainViewModel::class.java] }
    private val adapter: AppListAdapter = AppListAdapter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        initView()
        initObserver()
        initData()

        EventBus.getDefault().register(this)
    }

    private fun initView() {
        binding.tvTitle.text = resources.getString(R.string.app_list_title)
        Utils.tintDrawable(binding.ivBtnMore.drawable, resources.getColor(R.color.second_4))
        Utils.tintDrawable(binding.ivBtnSearch.drawable, resources.getColor(R.color.second_4))

        binding.recyclerView.layoutManager = LinearLayoutManager(this)
        binding.recyclerView.adapter = adapter
        binding.swipeRefresh.setOnRefreshListener { mainViewModel.loadApplicationItems() }

        binding.tvFilterBtn.setOnClickListener { btn ->
            MainFilterPopupWindow(this)
                    .setData(mainViewModel.appFilter)
                    .showAsDropDown(btn)
        }
        binding.ivBtnMore.setOnClickListener { btn ->
            MainMorePopupWindow(this)
                    .showAsDropDown(btn, 0, DisplayUtil.dip2px(this, -15f), Gravity.BOTTOM)
        }
        binding.tvCategoryBtn.setOnClickListener {
            LabelManagerFragment.start(this, true)
        }
    }

    override fun onResume() {
        super.onResume()
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    private fun initObserver() {
        mainViewModel.observeAppList(this, { apps ->
            adapter.setAppList(apps)
            binding.swipeRefresh.isRefreshing = false
        })
        mainViewModel.observeAppFilter(this, { filter ->
            if (filter.isDefault) {
                binding.tvTitleFilter.visibility = View.GONE
            } else {
                binding.tvTitleFilter.visibility = View.VISIBLE
                val sb = StringBuilder()
                filter.toStrList(this).forEach { sb.append(it).append(",") }
                binding.tvTitleFilter.text = sb.substring(0, sb.length - 1)
            }
        })
    }

    private fun initData() {
        binding.swipeRefresh.isRefreshing = true
        mainViewModel.loadApplicationItems()
    }

    @Subscribe
    fun onEvent(event: AppFilterUpdateEvent) {
        mainViewModel.appFilter = event.filter
        initData()
    }

}